<?php
$loader->requireOnce('includes/AbsReportTemplate.class.php');
$loader->requireOnce('includes/paymentLogging/PaymentLogger.class.php');
/**
 * Revenue by doctor
 *
 * @author ptnguyen
 *
 */
class Report02 extends AbsReportTemplate {

    private $data  = array() ;
	private $total = 0;
	private $totalCollecting   =   0;


    function __construct($name) {
        parent::__construct($name);
    }

    /**
     * (non-PHPdoc)
     * @see modules/report/local/includes/AbsReportTemplate#buildReportModel()
     */
    function buildReportModel() {
        $start  =   $this->startDate . " 00:00:00";
        $end    =   $this->endDate . " 23:59:59";

        /**
         * Get all receipt item
         * Tai khoan du - doctor_id = -1
         *
         */
        $ds =   new Datasource_sql();
        $sql    =   array(
            'cols'  =>  'pri.doctor_id, pri.enum_item_type, pri.amount',
            'from'  =>  'pay_receipt pr
            		INNER JOIN pay_receipt_item pri ON pr.pay_receipt_id = pri.pay_receipt_id',
            'where' =>  "
            	pr.created_time >= '$start'
            	AND pr.created_time <= '$end'"
        );
        $ds->setup($this->_db, $sql);        
        $res = $ds->toArray();
        $revenue = new DoctorRevenue();
        foreach ($res as $item) {
            $revenue->addItem($item['doctor_id'], $item['enum_item_type'], $item['amount']);
        }

        $this->data = $revenue->getRevenue();
        $this->total = $revenue->getTotal();
    }

    /**
     * (non-PHPdoc)
     * @see modules/report/local/includes/AbsReportTemplate#renderReportHtml()
     */
    function renderReportHtml() {
        $this->_controller->assign('title', $this->name);
		$this->_controller->assign('date', $this->getDate());
		$this->_controller->assign('data', $this->data);

		$this->_controller->assign('total', L10nHelper::renderNumber($this->total));
		$this->_controller->assign('index', 1);
		
		$reportLogger = new PaymentLogger($this->_controller, $this->startDate, $this->endDate);
        $reportLogger->setReportType(PaymentLogger::REPORT_BY_DOCTOR);
        $reportLogger->buildLogModel();
        $this->_controller->assign('loggerInfo', $reportLogger->renderLogInfo());
		
		if ($this->_print) {
			return $this->_controller->view->render('print.html');
		}
		return $this->_controller->view->render('view.html');
    }
}

/**
 TRUNCATE TABLE `pay_payment`;
TRUNCATE TABLE `pay_payment_item`;
TRUNCATE TABLE `pay_receipt_log`;
TRUNCATE TABLE `pay_receipt`;
TRUNCATE TABLE `pay_receipt_item`;

 */

class DoctorRevenue {

    const CREDIT_ACCOUNT      = 'credit_account';
    const XRAY_ACCOUNT        = 'xray';
    const LAB_ACCOUNT         = 'lab';

    const CREDIT_TITLE        = 'Tài khoản BN';
    const XRAY_TITLE          = 'X-quang';
    const LAB_TITLE           = 'Xét nghiệm';

    /**
     *
     * @var Array
     * 	key 	=> doctor_id
     *  value 	=> doctor_name
     */
    private $employeeList;
    private $otherAccountList = array(
        self::XRAY_ACCOUNT      => self::XRAY_TITLE,
        self::LAB_ACCOUNT       => self::LAB_TITLE,
        self::CREDIT_ACCOUNT    => self::CREDIT_TITLE);

    private $total = 0;
    private $doctorRevenue = array();
    private $otherRevenue = array(
        self::LAB_ACCOUNT    => 0,
        self::XRAY_ACCOUNT   => 0,
        self::CREDIT_ACCOUNT => 0);

    public function __construct() {
        $this->employeeList = Celini::newOrdo('Employee')->getEmployeeList(0, 0, 'all', true);
    }

    public function addItem($doctorId, $serviceType, $amount) {
        $this->total += $amount;

        // Credit account has one of the following value
        // doctor_id = -1
        // service_id = -1
        // enum_item_type (serviceType) = -1

	    if ($doctorId == -1) {
	        $this->otherRevenue[self::CREDIT_ACCOUNT] += $amount;
	        return;
	    }

        switch ($serviceType) {
            // Laboratory
        	case EnumerationConstant::ODON_SERVICE_TYPE_LAB_KEY:
        	    $this->otherRevenue[self::LAB_ACCOUNT] += $amount;
        	    break;

       	    // X-ray
        	case EnumerationConstant::ODON_SERVICE_TYPE_X_RAY_KEY:
        	    $this->otherRevenue[self::XRAY_ACCOUNT] += $amount;
        	    break;

        	// High-tech service
        	case EnumerationConstant::ODON_SERVICE_TYPE_HIGH_TECH_KEY:
        	    if (array_key_exists($doctorId, $this->doctorRevenue)) {
        	        $this->doctorRevenue[$doctorId] += $amount;
        	    } else {
        	        $this->doctorRevenue[$doctorId] = $amount;
        	    }
        	    break;

        	default:
        	    return;
        }
        // End switch
    }

    public function getRevenue() {
        $result = array();

        $doctorList = array_keys($this->doctorRevenue);
        foreach ($doctorList as $doctorId) {
            $result[] = array(
                'doctor_id'    => $doctorId,
                'doctor_name'  => $this->employeeList[$doctorId],
                'amount'       => L10nHelper::renderCurrency($this->doctorRevenue[$doctorId]));
        }

        $accountList = array_keys($this->otherRevenue);
        foreach($accountList as $accountKey) {
            $result[] = array(
                'doctor_id'    => $accountKey,
                'doctor_name'  => $this->otherAccountList[$accountKey],
                'amount'       => L10nHelper::renderCurrency($this->otherRevenue[$accountKey]));
        }

        return $result;
    }

    public function getTotal() {
        return $this->total;
    }
}


















